﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons;
using DAL;
using System.Web.Security;
using DAL.Enums;
using SimpComm;

namespace Bll
{
    public class AdmInfo
    {
        private static AdmInfo instance;

        private AdmInfo() { }

        public static AdmInfo Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AdmInfo();
                }
                return instance;
            }
        }
        #region 声明变量

        /// <summary>
        /// 管理员ID
        /// </summary>
        public int AdmID
        {
            get
            {
                if (ContextFactory.Current.Session["AdmID"] != null)
                    return ConvertHelper1.ToInt32(ContextFactory.Current.Session["AdmID"], 0);
                return 0;
            }
            set { ContextFactory.Current.Session.Add("AdmID", value); }
        }

        /// <summary>
        /// 管理员权限
        /// </summary>
        public int AdmRole
        {
            get
            {
                if (ContextFactory.Current.Session["AdmRole"] != null)
                    return ConvertHelper1.ToInt32(ContextFactory.Current.Session["AdmRole"], 0);
                return 0;
            }
            set { ContextFactory.Current.Session.Add("AdmRole", value); }
        }

        /// <summary>
        /// 管理员姓名
        /// </summary>
        public string AdmName
        {
            get
            {
                if (ContextFactory.Current.Session["AdmName"] != null)
                    return ContextFactory.Current.Session["AdmName"].ToString().Trim();
                return null;
            }
            set { ContextFactory.Current.Session.Add("AdmName", value); }
        }


        /// <summary>
        /// 管理员权限列表
        /// </summary>
        public string ColKeys
        {
            get
            {
                if (ContextFactory.Current.Session["ColKeys"] != null)
                    return ContextFactory.Current.Session["ColKeys"].ToString().Trim();
                return null;
            }
            set { ContextFactory.Current.Session.Add("ColKeys", value); }
        }
        #endregion

        /// <summary>
        /// 是否登录有效
        /// </summary>
        /// <returns></returns>
        public bool IsAuthenticated
        {
            get
            {
                if (AdmID > 0)
                    return true;
                return false;
            }
        }

        public int AdmMsgCount { get; set; }

        #region 登录类
        /// <summary>
        /// 登录类
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="lx">保存时间，0：不保存</param>
        /// <returns></returns>
        public MsgBox Login(string Security_LoginID, string Security_LoginPwd, bool IsWriteLog = true)
        {
            Security_LoginPwd = MD5Util.GetMD5_32(Security_LoginPwd);
            if (string.IsNullOrWhiteSpace(Security_LoginID) || string.IsNullOrWhiteSpace(Security_LoginPwd))
                return new MsgBox(false, "账号密码不能为空或者非法值");
            Adm Adm = new AdmService().Dal.GetEntity(p => p.Security_LoginID == Security_LoginID && p.Security_LoginPwd == Security_LoginPwd);
            if (Adm == null)
            {
                return new MsgBox(false, "密码或账号不正确.");
            }
            if (IsWriteLog)
            {
                var n = new Log_Adm()
                            {
                                Action = emAdmLogAction.LogOn.Value,
                                AdmID = Adm.ID,
                            };
                new Log_Adm_Service().Insert(n);
            }
            AdmID = Adm.ID;
            AdmName = Adm.Name;
            ColKeys = Adm.ColKeys;
            AdmRole = Adm.Role;
            return new MsgBox(true, "登录成功");
        }
        #endregion
    }

}